<?php
		$servername="DRIVER={SQL Server};SERVER=10.100.130.155;DATABASE=SmartClient";
		$conn=odbc_connect($servername,'thtfmes',"Mes&thtf",SQL_CUR_USE_ODBC);
		if (!$conn){
			echo "<font color='red'>Couldn't connect to SQL Server on $servername </font>";  
		}else{
			$sql = "select count(*) as countnum from ProductKey where ProductKeyStateID=1";
			$exec=odbc_exec($conn, $sql);

				while (odbc_fetch_row($exec)){
					$num=odbc_result($exec,"countnum");
					echo "$num\r\n";
				}
			odbc_close($conn);
		}

		$to = "Paladin.Jiang@hd.tongfangpc.com;jianghua.wei@hd.tongfangpc.com;A-TFS-ENG-EE-TE@hd.tongfangpc.com;Pei.lu@hd.tongfangpc.com;Amanda.Fang@hd.tongfangpc.com";
		$title = "【维修KEY预警】: 计算机维修KEY数量不足20个";

		$Message = "Dear All,<br />    发现计算机维修KEY数量不足<br /><br />
					<b>    当前数量:</b><font color='red'>${num}</font>个，不足20个<br />请及时<b>购买</b>补充，以免无KEY可用！！！";

		if($num<20) {
			$headers = "X-Mailer:PHP\n";
			$headers.= "MIME-Version: 1.0\n";
			$headers.= "Content-type:text/html;charset=utf-8\n";
			$headers.= "X-Priority: 1\n";
			$headers.= "From: MFG_MON<MFG_MON@hd.tongfangpc.com>\r\n"; 
			$message = "<html><head><meta charset='utf-8'></head><body>${Message}</body></html>";
			$subject = "${title}";

			$message=wordwrap($message, 70);
			
			if (mail($to, $subject, $message, $headers)) {
				echo " - 邮件发送成功!";
			} else {
				mail($to, $subject, $message, $headers);
			}
		}
?>
